package com.cxy.controller;

import com.cxy.pojo.Emp;
import com.cxy.pojo.Result;
import com.cxy.service.EmpService;
import com.cxy.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/login")
public class LoginController {
    @Autowired
    private EmpService empService;
    @Autowired
    private JwtUtils jwtUtils;

    @PostMapping
    public Result login(@RequestBody Emp emp){
        log.info("员工登入：{}" ,emp);

        Emp e = empService.login(emp);

        //登陆成功返回Jwt令牌，登陆失败返回失败信息
        if(e != null){
            Map<String, Object> claims = new HashMap<>();
            claims.put("id",e.getId());
            claims.put("name", e.getName());
            claims.put("username",e.getUsername());
            String jwt = jwtUtils.generateJwt(claims);
            return Result.success(jwt);
        }else {
            return Result.error("用户名或密码错误");
        }
    }
}
